<?php

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

//档案信息
function getprof($id) {
	global $_SGLOBAL;
	
	$query = $_SGLOBAL['db']->query("SELECT m.*, u.* FROM ".tname('mtag')." m , ".tname('member')." u WHERE m.tagtype='1' AND m.tagid='$id' AND m.moderator=u.uid");
	if(!$mtag = $_SGLOBAL['db']->fetch_array($query)) {
		showmessage('designated_election_it_does_not_exist');
	}
	//空群组
	if($mtag['membernum']<1 && ($mtag['joinperm'] || $mtag['viewperm'])) {
		$mtag['joinperm'] = $mtag['viewperm'] = 0;
		updatetable('mtag', array('joinperm'=>0, 'viewperm'=>0), array('tagid'=>$id));
	}
	
	//处理
	include_once(S_ROOT.'./data/data_profield.php');
	$mtag['field'] = $_SGLOBAL['profield'][$mtag['fieldid']];
	$mtag['title'] = $mtag['field']['title'];
	if(empty($mtag['pic'])) {
		$mtag['pic'] = 'image/nologo.jpg';
	}

	//成员级别
	$mtag['ismember'] = 0;
	$mtag['grade'] = -9;//-9 非成员 -2 申请 -1 禁言 0 普通 1 明星 8 副群主 9 群主
	$query = $_SGLOBAL['db']->query("SELECT grade FROM ".tname('tagspace')." WHERE tagid='$id' AND uid='$_SGLOBAL[supe_uid]' LIMIT 1");
	if($value = $_SGLOBAL['db']->fetch_array($query)) {
		$mtag['grade'] = $value['grade'];
		$mtag['ismember'] = 1;
	}
	if($mtag['grade'] < 9 && checkperm('managemtag')) {
		$mtag['grade'] = 9;
	}
	$mtag['allowpost'] = 1;
	$mtag['allowview'] = 1;
	
	$mtag['allowinvite'] = $mtag['grade']>=0?1:0;
	if($mtag['joinperm'] && $mtag['grade'] < 8) {
		$mtag['allowinvite'] = 0;
	}
	
	return $mtag;
}

function getspot2prof($spotid) {
    global $_SGLOBAL;

    $sql = "SELECT pic, tagname, tagid FROM ".tname('mtag')." WHERE tagid IN (SELECT tag1 FROM ".tname('spot')." WHERE spotid=".$spotid." UNION (SELECT tag2 FROM ".tname('spot')." WHERE spotid=".$spotid."))";

    $query = $_SGLOBAL['db']->query($sql);
	while ($spotpic = $_SGLOBAL['db']->fetch_array($query)) {
        if(empty($spotpic['pic'])) {
			$spotpic['pic'] = 'image/nologo.jpg';
		}
            
        $spotpics[] = $spotpic;
    }

    return $spotpics;
}

function getprofpics($profid, $pageno, $perpage = 1) {
    global $_SGLOBAL;

    $sql = "SELECT DISTINCT p.* FROM ".tname('profpic')." pp, ".tname('pic')." p WHERE pp.profid=".$profid."  ORDER BY p.dateline DESC LIMIT $pageno, $perpage";
    
    //
    $query = $_SGLOBAL['db']->query($sql);
	while ($pic = $_SGLOBAL['db']->fetch_array($query)) {
        $pics[] = $pic;
    }
    //echo count($pics);
    return count($pics) > 0 ? ((count($pics) == 1) ? $pics[0] : $pics)  : NULL;
}

function getpicprofs($picid) {
	global $_SGLOBAL;

    /*
     *  TODO: here will be one sql statement.
     *
     */

	$sql = "SELECT DISTINCT t.* FROM ".tname('mtag')." t, ".tname('profpic')." pp WHERE t.tagid=pp.profid AND pp.picid = ".$picid." ORDER BY pp.dateline DESC";
    //
	$query = $_SGLOBAL['db']->query($sql);
	while ($prof = $_SGLOBAL['db']->fetch_array($query)) {
        $prof['picurl'] = $prof['pic'] ? $prof['pic'] : "image/nologo.jpg";
        $profs[] = $prof;
    }
	return $profs;
}



function getspotpics($spotid, $pageno , $perpage = 1) {
    global $_SGLOBAL;

    $sql = "SELECT DISTINCT p.* FROM ".tname('profpic')." pp, ".tname('pic')." p WHERE p.picid=pp.picid AND pp.profid IN (SELECT tag1 FROM ".tname('spot')." WHERE spotid=".$spotid.") AND pp.picid IN (SELECT picid FROM ".tname('profpic')." WHERE profid IN (SELECT tag2 FROM ".tname('spot')." WHERE spotid=".$spotid.")) ORDER BY p.dateline DESC LIMIT $pageno, $perpage";

	//echo $sql . "</br>";
    $query = $_SGLOBAL['db']->query($sql);
	while ($pic = $_SGLOBAL['db']->fetch_array($query)) {
        $pics[] = $pic;
    }
    return count($pics) > 0 ? ((count($pics) == 1) ? $pics[0] : $pics)  : NULL;
}

function getspotpic($spotid, $picid, $order=1) {
    global $_SGLOBAL;

    $sql = "SELECT * FROM ".tname('pic')." WHERE picid IN (SELECT DISTINCT p.picid FROM ".tname('profpic')." pp, ".tname('pic')." p WHERE p.picid=pp.picid AND pp.profid IN (SELECT tag1 FROM ".tname('spot')." WHERE spotid=".$spotid.") AND pp.picid IN (SELECT picid FROM ".tname('profpic')." WHERE profid IN (SELECT tag2 FROM ".tname('spot')." WHERE spotid=".$spotid."))) AND ". ($order == 1 ? "picid>$picid ORDER BY picid LIMIT 1" : "picid<$picid ORDER BY picid DESC LIMIT 1");

    //echo $sql . "</br>";
    $query = $_SGLOBAL['db']->query($sql);
    return $_SGLOBAL['db']->fetch_array($query);
}

function getprofpic($profid, $picid, $order=1) {
    global $_SGLOBAL;

    $sql = "SELECT DISTINCT p.* FROM ".tname('profpic')." pp LEFT JOIN ".tname('pic')." p ON pp.picid=p.picid WHERE pp.profid=".$profid."  AND ". ($order == 1 ? "p.picid>$picid ORDER BY p.picid LIMIT 1" : "p.picid<$picid ORDER BY p.picid DESC LIMIT 1");

    //echo $sql . "</br>";
    $query = $_SGLOBAL['db']->query($sql);
    return $_SGLOBAL['db']->fetch_array($query);
}

function getspotpiccnt($spotid) {
    global $_SGLOBAL;

    $sql =  "SELECT count(*) FROM (SELECT DISTINCT p.* FROM ".tname('profpic')." pp, ".tname('pic')." p WHERE  p.picid=pp.picid AND pp.profid IN (SELECT tag1 FROM ".tname('spot')." WHERE spotid=".$spotid.") AND pp.picid IN (SELECT picid FROM ".tname('profpic')." WHERE profid IN (SELECT tag2 FROM ".tname('spot')." WHERE spotid=".$spotid.")) ORDER BY p.dateline DESC) AS count";

    $result = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($sql), 0);

    return $result;
}

function getpicseq($id, $picid, $type = 'spot') {
    global $_SGLOBAL;

    if ($type == 'spot') {
       $tsql = "SELECT DISTINCT p.picid FROM ".tname('profpic')." pp, ".tname('pic')." p WHERE p.picid=pp.picid AND pp.profid IN (SELECT tag1 FROM ".tname('spot')." WHERE spotid=".$id.") AND pp.picid IN (SELECT picid FROM ".tname('profpic')." WHERE profid IN (SELECT tag2 FROM ".tname('spot')." WHERE spotid=".$id."))";   
    } else {
        $tsql = "SELECT DISTINCT p.picid FROM ".tname('profpic')." pp LEFT JOIN ".tname('pic')." p ON pp.picid=p.picid WHERE pp.profid=".$id; 
    }
    
    $sql = "SELECT COUNT(*) as cnt FROM ".tname('pic')." WHERE picid IN (".$tsql.") AND picid>$picid ORDER BY picid DESC";


    $query = $_SGLOBAL['db']->query($sql);
    return $_SGLOBAL['db']->result($query, 0) + 1; 
}

function getprofspotcntbyfid($profid, $fid=0) {
    global $_SGLOBAL;

    $sql = " SELECT DISTINCT p.*, pf.parentid FROM ".tname('mtag')." p 
         LEFT JOIN ".tname('spot')." sp ON p.tagid=sp.tag1 OR p.tagid=sp.tag2 
         LEFT JOIN ".tname('profield')." pf ON p.fieldid=pf.fieldid 
         WHERE p.tagid<>$profid AND (sp.tag1=$profid OR sp.tag2=$profid)";

     $tsql =  "SELECT p.*, pf.* FROM ".tname('mtag')." p 
        LEFT JOIN ".tname('profield')." pf ON p.fieldid=pf.fieldid WHERE pf.fieldid IN (SELECT fieldid FROM ".tname('profield')." WHERE parentid=$fid)";

     $sql = $sql . ($fid ? " AND p.fieldid IN ($tsql) " : '');

     $query = $_SGLOBAL['db']->query($sql);
     while ($avalue = $_SGLOBAL['db']->fetch_array($query)) {
        if ($avalue['parentid']) {
            $spotcount[$avalue['parentid']] += 1; 
        }
     }
//    print_r($spotcount);
    return $spotcount; 
}

// $type = 0 means order by pic_cnt, $type = 1 mans order by dateline
function getprofs($fid=0, $type=0, $start=0, $perpage=6) {
    global $_SGLOBAL;

    $proflist = array();

    $sql =  "SELECT t.*, ppf.* FROM ".tname('mtag')." t 
        LEFT JOIN ".tname('profield')." ppf ON t.fieldid=ppf.fieldid 
        WHERE 1  ";

    $tsql =  " AND ppf.fieldid IN (SELECT fieldid FROM ".tname('profield')." WHERE parentid=$fid)";
    //echo $tsql . "<br>";
    $sql .= $fid ? "$tsql" : '';
    $sql .= $type == 0 ? " ORDER BY t.spotcnt DESC" : " ORDER BY t.dateline DESC";
    $sql .= " LIMIT ".$start.", ".$perpage."";

//    echo $sql . "<br>";

    $query = $_SGLOBAL['db']->query($sql);
    while ($avalue = $_SGLOBAL['db']->fetch_array($query)) {
        if(empty($avalue['pic'])) {
			$avalue['pic'] = 'image/nologo.jpg';
		}
        $proflist[] = $avalue;
    }

    return $proflist;
}

// $type = 0 means order by pic_cnt, $type = 1 mans order by dateline
function getspots($fid=0, $type=0, $start=0, $perpage=6) {
     global $_SGLOBAL;
     $catelist = array();
     $sql = "SELECT DISTINCT sp.*, p.*, t.tagname AS t1, mt.tagname AS t2, t.pic AS p1, mt.pic AS p2 FROM ".tname('pic')." p, ".tname('spot')." sp , ".tname('mtag')." t , ".tname('mtag')." mt WHERE sp.tag2=mt.tagid AND sp.tag1=t.tagid AND sp.last_picid=p.picid AND sp.last_picid !='0' ";
     $tsql =  "SELECT t.tagid FROM ".tname('mtag')." t 
        LEFT JOIN ".tname('profield')." ppf ON t.fieldid=ppf.fieldid WHERE ppf.fieldid IN (SELECT fieldid FROM ".tname('profield')." WHERE parentid=$fid) OR ppf.fieldid=$fid";
     //echo $tsql . "<br>";
     $sql .= $fid ? " AND sp.tag1 IN ($tsql) AND sp.tag2 IN ($tsql)" : '';
     $sql .= $type == 0 ? "  ORDER BY sp.pic_cnt DESC" : " ORDER BY sp.dateline DESC";
     $sql .= "  LIMIT ".$start.", ".$perpage."";
     
     //echo $sql . "<br>";
     $query = $_SGLOBAL['db']->query($sql);
     while ($avalue = $_SGLOBAL['db']->fetch_array($query)) {
        $avalue['tag1pic'] = $avalue['p1'] ? $avalue['p1'] : "image/nologo.jpg";
        $avalue['tag2pic'] = $avalue['p2'] ? $avalue['p2'] : "image/nologo.jpg";
        $avalue['lastpic'] = mkpicurl($avalue, 0);
        $space = getspace($avalue['uid']);
        $avalue['spotter'] = $space;
        $catelist[] = $avalue; 
     }
     //print_r($catelist);
     return $catelist;
}

function getprofspotsbyfid($profid, $fid=0, $start=0, $perpage=20) {
     global $_SGLOBAL;

     $catelist = array();
    
     $sql = "SELECT DISTINCT p.*, sp.*, pf.* FROM ".tname('mtag')." p 
         LEFT JOIN ".tname('spot')." sp ON p.tagid=sp.tag1 OR p.tagid=sp.tag2 
         LEFT JOIN ".tname('profield')." pf ON p.fieldid=pf.fieldid
         WHERE p.tagid<>$profid AND (sp.tag1=$profid OR sp.tag2=$profid)";

    
     $tsql =  "SELECT p.fieldid FROM ".tname('mtag')." p 
        LEFT JOIN ".tname('profield')." pf ON p.fieldid=pf.fieldid WHERE pf.fieldid IN (SELECT fieldid FROM ".tname('profield')." WHERE parentid=$fid)";

     $sql = $sql . ($fid ? " AND p.fieldid IN ($tsql) " : '')." LIMIT $start, $perpage";


     $query = $_SGLOBAL['db']->query($sql);
     while ($avalue = $_SGLOBAL['db']->fetch_array($query)) {
	if ($avalue['last_picid'] != 0) {
	    $sql = "SELECT p.* FROM ".tname('spot')." sp, ".tname('pic')." p WHERE sp.last_picid=p.picid AND sp.last_picid=".$avalue['last_picid'];
	    $q = $_SGLOBAL['db']->query($sql);
	    $last_pic = $_SGLOBAL['db']->fetch_array($q);
	}           
	$avalue['pic'] = $avalue['last_picid'] != '0'  ? mkpicurl($last_pic, 0) : "image/nologo.jpg";
	$sql = "SELECT p1.tagname AS t1name, p1.pic AS t1pic, p2.tagname AS t2name, p2.pic AS t2pic  FROM ".tname('spot')." sp  LEFT JOIN ".tname('mtag')." p1 ON sp.tag1=p1.tagid  
			LEFT JOIN ".tname('mtag')." p2 ON sp.tag2=p2.tagid WHERE sp.spotid=". $avalue['spotid'];
	$q1 = $_SGLOBAL['db']->query($sql);
	$v = $_SGLOBAL['db']->fetch_array($q1);
	$avalue['p1pic'] = $v['t1pic']?$v['t1pic']:"image/nologo.jpg";
	$avalue['p2pic'] = $v['t2pic']?$v['t2pic']:"image/nologo.jpg";
	$avalue['p1name'] = $v['t1name'];
	$avalue['p2name'] = $v['t2name'];
        if ($avalue['parentid']) {
            $catelist[$avalue['parentid']][] = $avalue; 
        } else {
            $catelist[$avalue['fieldid']][] = $avalue;
        }
     }
     return $catelist;
}


// the thread of wtming, exclude the original mtag
function get_hot_wthread_list($fid=0, $start=0, $pagenum=6)
{
    global $_SGLOBAL;

    //最新话题
    $sql = "SELECT tid,subject,uid,username, p.tagid, p.tagname FROM ".tname('thread')." t LEFT JOIN ".tname('mtag')." p ON p.tagid=t.tagid WHERE  p.tagtype='1' ";
    $tsql =  "SELECT p.fieldid FROM ".tname('mtag')." p 
        LEFT JOIN ".tname('profield')." pf ON p.fieldid=pf.fieldid WHERE pf.fieldid IN (SELECT fieldid FROM ".tname('profield')." WHERE parentid=$fid) OR pf.fieldid=$fid";
     //echo $tsql . "<br>";
    $sql = $sql . ($fid ? " AND p.fieldid IN ($tsql) " : '')." ORDER BY t.replynum DESC, t.viewnum DESC, t.dateline DESC LIMIT $start,$pagenum";
	$query2 = $_SGLOBAL['db']->query($sql);
	while ($thread = $_SGLOBAL['db']->fetch_array($query2)) {
		$pthreadlist[] = $thread;
	}
    return $pthreadlist;
}

function gen_nav_txt_by_id($fid) {
    global $_SGLOBAL;

	$parent_fid_txt = '';
    $parent_fid = $_SGLOBAL['profield'][$fid]['parentid'];
	if (array_key_exists($parent_fid, $_SGLOBAL['profield'])) {
		$parent_fid_txt = "&gt;&gt; <a href=\"site.php?ac=profile&op=list&fid=".$_SGLOBAL['profield'][$parent_fid]['fieldid']."\">" . $_SGLOBAL['profield'][$parent_fid]['title'] . "</a> ";
        $parent_fid_txt = gen_nav_txt_by_id($parent_fid) . $parent_fid_txt;
    }

    return $parent_fid_txt;
}

function get_parent_name_by_fid($fid, $type="title")
{
	global $_SGLOBAL;
	$parent_fid = $_SGLOBAL['profield'][$fid]['parentid'];
	if (!array_key_exists($fid, $_SGLOBAL['profield'])) {
		return null;
	}

	if (!$parent_fid)
	{
		if($type == "title")
			$nav = $_SGLOBAL['profield'][$fid]['title'];
		elseif($type == "id")
			$nav = $_SGLOBAL['profield'][$fid]['fieldid'];
	}
	else
		$nav = get_parent_name_by_fid($parent_fid, $type);
	return $nav;
}


function get_li_name_by_fid($fid)
{
	 global $_SGLOBAL;

	 $parent_fid = $_SGLOBAL['profield'][$fid]['parentid'];

	 if (!array_key_exists($fid, $_SGLOBAL['profield']))
		return null;

	 if (!$parent_fid)
	 {
		 if ($fid == 4)
			 return 'star';
		 if ($fid == 66)
			 return 'brand';
		 if ($fid == 67)
			 return 'product';
	 }
	 if (!array_key_exists($parent_fid, $_SGLOBAL['profield']))
		 return null;

	 $fid = $_SGLOBAL['profield'][$parent_fid]['fieldid'];

	 if ($fid == 4)
			 return 'star';
	 if ($fid == 66)
			 return 'brand';
     if ($fid == 67)
			 return 'product';
}
?>
